package com.shop.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.shop.commom.utils.JSONResult;
import com.shop.pojo.UserAddress;
import com.shop.service.UserAddressService;
import com.shop.vo.JSONResponse;
import com.shop.vo.UserAddressSaveRequest;
import com.shop.vo.UserAddressUpdateRequest;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 用户地址表 
 *
 * @author yukangkang
 * @email sunlightcs@gmail.com
 * @date 2020-10-30 13:16:29
 */
@RestController
@RequestMapping("address")
public class UserAddressController {

    @Autowired
    private UserAddressService userAddressService;


    @PostMapping("/list")
    public JSONResult list(@RequestParam String userId){
        QueryWrapper<UserAddress> query = new QueryWrapper<>();
        query.eq(UserAddress.USER_ID,userId);
        List<UserAddress> list = userAddressService.list(query);
        return JSONResult.OK(list);
    }



    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "详情")
    public JSONResponse info(@PathVariable("id") String id){
		UserAddress userAddress = userAddressService.getById(id);
        return JSONResponse.ok(userAddress);
    }

    /**
     * 保存
     */
    @PostMapping("/add")
    @ApiOperation(value = "保存")
    public JSONResult save(@RequestBody UserAddressSaveRequest request){
		userAddressService.save(request);
        return JSONResult.OK();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public JSONResult update(@RequestBody UserAddressUpdateRequest request){
		userAddressService.update(request);
        return JSONResult.OK();
    }

    /**
     * 修改
     */
    @PostMapping("/setDefalut")
    @ApiOperation(value = "修改")
    public JSONResult setDefalut(@RequestParam("userId") String userId,@RequestParam("addressId") String addressId){
		userAddressService.setDefalut(userId,addressId);
        return JSONResult.OK();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public JSONResult delete(@RequestParam String addressId){
		userAddressService.delete(addressId);
        return JSONResult.OK();
    }

}
